package com.haier.nengyuan.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

/**
 * Created by wy on 2017/8/28.
 */

public abstract class BaseFragment extends Fragment {
    public String tag = "xx";
    public Activity mActivity;
    public LayoutInflater mLayoutInflater;
    public View mRootView;
    public FragmentManager mFragmentManager;

    public BaseFragment() {
        tag = this.getClass().getSimpleName();
    }

    //日累计收益
    protected float[][] points = new float[][]{{0, 0}, {2, 0}, {4, 0}, {6, 0.12f}, {8, 1.96f}, {10, 6.8f},
            {12, 5.24f}, {14, 7.1f}, {16, 4.34f}, {18, 1.13f}, {20, 0}, {22, 0}, {24, 0}};
    protected float[][] points1 = new float[][]{{0, 0}, {2, 0}, {4, 0}, {6, 3}, {8, 2}, {10, 8}, {12, 3},
            {14, 2}, {16, 1}, {18, 1}, {20, 0}, {22, 0}, {24, 0}};
    //日发电量
    public static float[][] points4 = new float[][]{{0, 0}, {2, 0}, {4, 0}, {6, 0.14f}, {8, 2.31f},
            {10, 6.17f}, {12, 8.54f}, {14, 8.38f}, {16, 5.11f}, {18, 1.33f}, {20, 0}, {22, 0}, {24, 0}};
    //周发电量
    protected float[][] points2 = new float[][]{{1, 10.43f}, {2, 16.08f}, {3, 26.69f}, {4, 14.81f}, {5, 15.91f},
            {6, 18.10f}, {7, 28.99f}};
    //周收益
    protected float[][] points5 = new float[][]{{1, 8.87f}, {2, 13.67f}, {3, 22.69f}, {4, 12.59f}, {5, 13.52f},
            {6, 15.39f}, {7, 24.64f}};
    protected float[][] points3 = new float[][]{{1, 52}, {2, 13}, {3, 51}, {4, 20}, {5, 19}, {6, 20}, {7, 54},
            {8, 7}, {9, 19},
            {10, 41}, {11, 41}, {12, 41}, {13, 41}, {14, 41}, {15, 41}, {16, 41}, {17, 41}, {18, 41}, {19, 41},
            {20, 41}, {21, 41}, {22, 41}, {23, 41}, {24, 41}};
    protected int[] mColors = {0xFFCCFF00, 0xFF6495ED, 0xFFE32636, 0xFF800000, 0xFF808000, 0xFFFF8C69, 0xFF808080,
            0xFFE6B800, 0xFF7CFC00};

    protected float pxTodp(float value) {
        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        float valueDP = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, metrics);
        return valueDP;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        mActivity = this.getActivity();
        mLayoutInflater = mActivity.getLayoutInflater();
        mFragmentManager = this.getFragmentManager();
        mRootView = getRootView(setRootView(), container);

        initViews();

        return mRootView;
    }

    public View getRootView(int resLayout, ViewGroup container) {
        View view = mLayoutInflater.inflate(resLayout, container, false);
        return view;
    }

    //设置布局
    public abstract int setRootView();

    //初始化布局View
    public abstract void initViews();


    //打吐司
    public void showToast(String text) {
        showToast(text, Toast.LENGTH_SHORT);
    }

    public void showToast(String text, int length) {
        Toast.makeText(mActivity, text, length).show();
    }

    //自杀
    public void killSelf() {
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        fragmentTransaction.remove(this);
        fragmentTransaction.commit();
    }

    //跳转到其他Activity
    public void startActvity(Class activity) {
        Intent intent = new Intent(mActivity, activity);
        mActivity.startActivity(intent);
    }

    //启动服务
    public void startService(Class service) {
        Intent intent = new Intent(mActivity, service);
        mActivity.startService(intent);
    }

    //发送广播
    public void sendBroadcast(Class receiver) {
        Intent intent = new Intent(mActivity, receiver);
        mActivity.sendBroadcast(intent);
    }
}
